package com.vonchenchen.mytest.RollLoadPhotoView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.vonchenchen.mytest.R;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by vonchenchen on 2015/10/28 0028.
 */
public class FlyBackground extends RelativeLayout{

    private Context mCtx;
    private ScaleRelativeLayout mTopView;
    private ScaleRelativeLayout mMidView;

    private ImageView mTopImageView;
    private ImageView mMidImageView;

    //存两张
    private List<Bitmap> mDispBitmapList = new LinkedList<Bitmap>();

    public FlyBackground(Context context) {
        super(context);
        this.mCtx = context;
        init();
    }

    public FlyBackground(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mCtx = context;
        init();
    }

    private void init(){

        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_qq);

        mDispBitmapList.add(bitmap1);
        mDispBitmapList.add(bitmap2);
    }

    private void initData(){
        mTopImageView.setImageBitmap(mDispBitmapList.get(0));
        mMidImageView.setImageBitmap(mDispBitmapList.get(1));
    }

    private void initView(){
        mTopView = (ScaleRelativeLayout)this.findViewById(R.id.rl_top);
        mTopImageView = (ImageView) this.findViewById(R.id.iv_top);
        mTopView.setIsTop(true);
        mMidView = (ScaleRelativeLayout)this.findViewById(R.id.rl_mid);
        mMidImageView = (ImageView) this.findViewById(R.id.iv_mid);
        mMidView.setIsTop(false);
    }

    private void initListener(){
        mTopView.setOnDismissListener(new ScaleRelativeLayout.OnDismissListener() {
            @Override
            public void onLeftDismiss(View view) {
                updateDispBitmapList();
                initData();
            }

            @Override
            public void onRightDismiss(View view) {
                updateDispBitmapList();
                initData();
            }

            @Override
            public void onReduction(View view) {

            }
        });
    }

    private void updateDispBitmapList(){
        Bitmap bitmapTmp = mDispBitmapList.remove(0);
        mDispBitmapList.add(bitmapTmp);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
        initListener();
        initData();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
